package com.example.x6.configurationmaintenance.Service;

import android.app.IntentService;
import android.content.Intent;
import android.support.annotation.Nullable;
import com.example.x6.configurationmaintenance.BoardCaseReceiver.BoardCaseReceiverUtil;
import com.example.x6.configurationmaintenance.DataBase.TimeTick.DatabaseHelper;
import com.example.x6.configurationmaintenance.DataBase.TimeTick.DatabaseManager;
import com.example.x6.configurationmaintenance.DataBase.TimeTick.DatabaseOpration;
import com.example.x6.configurationmaintenance.Interface.OnNearestTimeChangeListener;
import com.example.x6.configurationmaintenance.Utils.PowerKitDaemonUtil;
import com.example.x6.configurationmaintenance.Utils.RebootUtil;
import java.text.SimpleDateFormat;

public class RebootService extends IntentService {
    private final String TAG = getClass().getSimpleName();
    private String clk_uuid = null;
    private DatabaseManager databaseManager;
    private DatabaseOpration databaseOpration;
    private boolean isOneshot = false;
    private PowerKitDaemonUtil powerKitDaemonUtil;
    private boolean rebootLock = false;
    private long rebootclock = -2;

    public RebootService() {
        super("RebootService");
    }

    public void onCreate() {
        super.onCreate();
    }

    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    /* JADX INFO: Multiple debug info for r12v10 int: [D('HH' int), D('intent' android.content.Intent)] */
    /* access modifiers changed from: protected */
    public void onHandleIntent(@Nullable Intent intent) {
        new BoardCaseReceiverUtil();
        this.powerKitDaemonUtil = new PowerKitDaemonUtil(this);
        this.powerKitDaemonUtil.startDaemon();
        DatabaseOpration.initializeInstance(this);
        this.databaseOpration = DatabaseOpration.getInstance();
        DatabaseManager.initializelnstance(new DatabaseHelper(this));
        this.databaseManager = DatabaseManager.getInstance();
        this.databaseOpration.setOnNearestTimeChangeListener(new OnNearestTimeChangeListener() {
            /* class com.example.x6.configurationmaintenance.Service.RebootService.AnonymousClass1 */

            @Override // com.example.x6.configurationmaintenance.Interface.OnNearestTimeChangeListener
            public void onNearestTimeChanged(long nearestTime, boolean isOneShot, String uuid) {
                if (!RebootService.this.rebootLock) {
                    RebootService.this.rebootclock = nearestTime;
                    RebootService.this.isOneshot = isOneShot;
                    RebootService.this.clk_uuid = uuid;
                }
            }
        });
        while (true) {
            mDelay(1000);
            long currentTime = System.currentTimeMillis();
            int HH = Integer.parseInt(new SimpleDateFormat("HH").format(Long.valueOf(currentTime)));
            int mm = Integer.parseInt(new SimpleDateFormat("mm").format(Long.valueOf(currentTime)));
            int ss = Integer.parseInt(new SimpleDateFormat("ss").format(Long.valueOf(currentTime)));
            if (HH == 0 && mm == 0 && (ss == 0 || ss == 1)) {
                this.databaseOpration.getNearestTime();
            }
            if (Math.abs(this.rebootclock - currentTime) / 1000 < 2 && this.rebootclock - currentTime > 0) {
                this.rebootLock = true;
                if (this.isOneshot) {
                    this.databaseOpration.delete(this.clk_uuid);
                }
                RebootUtil.reboot(this);
                this.rebootLock = false;
            }
        }
    }

    private void mDelay(long mill) {
        try {
            Thread.sleep(mill);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
